package com.cash.channel.svc.service.common;

import com.cash.channel.svc.logic.ThirdCommonLogic;
import com.alibaba.fastjson.JSON;
import com.cash.channel.api.constant.Constant;
import com.cash.channel.api.model.ApiRequestModel;
import com.cash.channel.api.model.ApiResponseModel;
import com.cash.channel.api.model.request.BankCardListRequest;
import com.cash.channel.api.model.response.BankCardListResponse;
import com.cash.channel.svc.service.base.BankCardList;
import com.cash.channel.api.model.user.ThirdIdModel;
import com.cash.user.model.UserChannelCardPackModel;
import com.cash.user.service.UserChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service(Constant.CHANNEL.COMMON+ Constant.SERVICE.银行卡列表)
public class CommonBankCardList implements BankCardList {

    @Resource
    private UserChannelService userChannelService;

    @Resource
    private ThirdCommonLogic thirdCommonLogic;

    @Override
    public ApiResponseModel<BankCardListResponse> getBankCardList(ApiRequestModel apiRequestModel) {
        try {
            BankCardListRequest bankCardListRequest = JSON.parseObject(apiRequestModel.getData(), BankCardListRequest.class);
            BankCardListResponse bankCardListResponse = new BankCardListResponse();
            String uid = bankCardListRequest.getUserId();
            List<ThirdIdModel> thirdIdModels = thirdCommonLogic.findThirdIdModelByThirdUserId(uid, apiRequestModel.getChannel());
            List<UserChannelCardPackModel> userChannelCardPackModelList = userChannelService.findUserChannelCardPackListByUid(thirdIdModels.get(0).getCUserUid(), apiRequestModel.getChannel());
            for (UserChannelCardPackModel userChannelCardPackModel: userChannelCardPackModelList) {
                bankCardListResponse.addBankCard(userChannelCardPackModel.getcUid(), userChannelCardPackModel.getcIdno(), userChannelCardPackModel.getcName(),
                        userChannelCardPackModel.getcCardNo(), userChannelCardPackModel.getcBankCode(), userChannelCardPackModel.getcBankName(),
                        userChannelCardPackModel.getcReserveMobile(), userChannelCardPackModel.getnDefault(), userChannelCardPackModel.getcChannel());
            }
            return new ApiResponseModel<>(true, 0, "", bankCardListResponse);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new ApiResponseModel<>(false, -1, e.getMessage(), null);
        }
    }

}
